﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using ASBCC.Database.Entity;
using SourceCode.Workflow.Client;
using ASBCC.Database.Service;

namespace ASBCC.Workflow.K2Manager
{
    public class ForwardActionHandler : ActionHandlerBase
    {
        #region Constructor

        public ForwardActionHandler(SerializableDictionary dataFields, K2AppForm appForm, K2WorkItem workItem, K2ActionInfo actionInfo, string comment, K2AuditDestination auditDestination, List<K2User> outputDestinationUsers) :
            base(dataFields, appForm, workItem, actionInfo, comment, auditDestination, outputDestinationUsers)
        {
        }

        #endregion

        #region Override Methods
        /// <summary>
        /// 执行流程动作
        /// </summary>
        protected override void ExcuteAction()
        {
            Connection connection = K2Client.OpenConnection();
            try
            {
                ForwordWorkListItem();
            }
            finally
            {
                connection.Close();
            }
        }

        /// <summary>
        /// Forword动作时,重载图章生成方法,不需要生成图章
        /// </summary>
        protected override void BuildProcessStamp()
        {
        }

        protected override void SendMail()
        {
            string actionType = GetActionType();
            string ApplicationNameEn = FlowProcess.ProcDisplayEn;
            string ApplicationNameCn = FlowProcess.ProcDisplayCn;

            Flow_ProcessMailConfigTableEntity mailConfig = K2DataHelper.GetProcessMailConfig(WorkItem.ProcName, WorkItem.ActivityName, actionType);
            if (mailConfig == null) return;

            EZVisitApplicationTableEntity entity = EZVisitApplicationManager.SelectByKeys(AppForm.FormId);
            if (entity == null) return;

            string auditUser = (AppForm.DelegateFrom.UserId <= 0 || AppForm.DelegateFrom.UserId == AppForm.CurrentUser.UserId) ?
                    AppForm.CurrentUser.UserEn : string.Format("{0}({1})", AppForm.CurrentUser.UserEn, AppForm.DelegateFrom.UserEn);

            // 通知下一步审批人
            if (NewMyActivityList != null && NewMyActivityList.Count > 0)
            {
                foreach (MyActivityTableEntity myActivity in NewMyActivityList)
                {
                    if (myActivity.OwnerID == K2User.TempUser) continue; //临时用户不需要通知

                    int toAuditorTemplate = mailConfig.ToAuditorTemplate ?? 0;
                    if (toAuditorTemplate <= 0) continue;

                    EmployeeTableEntity ownerInfo = K2DataHelper.GetUserInfoById(myActivity.OwnerID ?? 0);

                    if (!CheckMailApproverRule(ownerInfo)) continue;

                    string[] mailAddressArr = new string[] { ownerInfo.Email };
                    NameValueCollection collection = new NameValueCollection();
                    collection["{EnglishName}"] = myActivity.OwnerEn;
                    collection["{ChineseName}"] = myActivity.OwnerCn;

                    if (!entity.EZVisitReqNoIsNull())
                        collection["{SerialNumber}"] = entity.EZVisitReqNo;
                    else
                        collection["{SerialNumber}"] = myActivity.ApplicationNo;
                    collection["{AuditUser}"] = auditUser;
                    collection["{FormURL}"] = myActivity.FormURL;
                    collection["{Comments}"] = Comment;
                    collection["{AuditTime}"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

                    NameValueCollection titleCollection = new NameValueCollection();
                    if (!entity.EZVisitReqNoIsNull())
                        titleCollection["{SerialNumber}"] = entity.EZVisitReqNo;
                    else
                        titleCollection["{SerialNumber}"] = myActivity.ApplicationNo;


                    MailService.MailSender(mailAddressArr, toAuditorTemplate, collection, titleCollection);
                }
            }
        }

        protected override string GetActionType()
        {
            return K2ActionType.Forward;
        }

        #endregion

        #region Help Methods
        private void ForwordWorkListItem()
        {
            string fromUserName = WorkItem.Destination;
            int toUserId = AuditDestination.ActivityDestinationList[0].UserArr[0];
            string toUser = K2Helper.GetDestinationName(toUserId);
            int procInstID = WorkItem.ProcInstId;
            int actInstDestID = WorkItem.ActInstDestId;
            K2Admin.RedirectWorklistItem(fromUserName, toUser, procInstID, actInstDestID, 0);
        }
        #endregion
    }
}
